<?php
class LetterController extends BaseController {
    protected $layout = 'layouts.center';
    public function send($id)
    {
    	try{
       		$user = User::findOrFail($id);
        	$message = Input::get('content');
	        if($message)
	        {
	            StationLetter::sendUserLetter($message, $user);
	        }
    	}catch ( Exception $ex ) {
			Log::error ( $ex );
			return $this->makeView ( 'errors.503', '服务器暂时无法响应。请过一段时间以后再试。', 503 );
		}
        return Redirect::to(Input::get('current_page'));
    }

	public function sys(){
		try{
            $page = Input::get('page', 1);
            $rows_per_page = Input::get('rows_per_page', PaginationHelper::ROWS_PER_PAGE);
            $offset = ($page-1) * $rows_per_page;
	    	$station_letter_sys = StationLetter::getSystemLetters($rows_per_page, $offset);
		}catch ( Exception $ex ) {
			Log::error ( $ex );
			return $this->makeView ( 'errors.503', '服务器暂时无法响应。请过一段时间以后再试。', 503 );
		}
		return $this->makeView('letter.sys',$station_letter_sys->toArray());
	}

    public function users()
    {
    	try{
        	$station_letter_users = StationLetterUser::getLetterUserList();
        }catch ( Exception $ex ) {
        		Log::error ( $ex );
        		return $this->makeView ( 'errors.503', '服务器暂时无法响应。请过一段时间以后再试。', 503 );
        }	
        return $this->makeView('letter.users',$station_letter_users->toArray());
    }

    public function user_letters($id)
    {
        $user = User::findOrFail($id);
        $letters = StationLetter::getUserLetters($id);
        foreach($letters as $letter)
        {
            if ($letter->creator_id == $user->id AND empty($letter->is_read))
            {
            	
                StationLetter::readLetter($letter->id);
            }
        }

        $grouped_letters = array();
//         $grouped_letters['user'] = $user->toArray();
//         $grouped_letters = $user->toArray();
        foreach($letters as $letter)
        {
            $date = strftime('%Y年 %m月 %d日', strtotime($letter->created_at));
            if (! isset($grouped_letters[$date]))
            {
                $grouped_letters[$date] = array();
            }
            $grouped_letters[$date][] = (array)$letter;
        } 
		return $this->makeView('letter.user-letters',array('grouped_letters' => $grouped_letters, 'user' => $user->toArray()));
    }

    public function sys_letters($id)
    {
    	try{
	        $sys_letter = StationLetter::find($id);
			StationLetter::readLetter($sys_letter->id);
    	}catch ( Exception $ex ) {
        		Log::error ( $ex );
        		return $this->makeView ( 'errors.503', '服务器暂时无法响应。请过一段时间以后再试。', 503 );
        }	
        return $this->makeView('letter.sys-letters',$sys_letter);
    }
    
    /**
     * 删除一条消息
     * 
     * @param int $id
     */
    public function delete($id)
    {
        $letter = StationLetter::find($id);
        if (!$letter) {
            return $this->makeView('errors.404', '该消息不存在', 404);
        }
        if ($letter->to != Auth::user()->id) {
            return $this->makeView('errors.401', '这个消息不是推送给你的，你没有权限删除', 401);
        }
        
        $letter->delete();
        
        return $this->makeView('letter.delete', "Successfully deleted a letter.");
    }

    private function _userName($user)
    {
        $customer = Customer::where('user_id', $user->id)->first();
        if ($customer)
        {
            return $customer->realname ? $customer->realname : $user->email;
        }
        $company = Company::where('user_id', $user->id)->first();
        if ($company)
        {
            return $company->name ? $company->name : $user->email;
        }
        $lawyer = Lawyer::where('user_id', $user->id)->first();
        if ($lawyer)
        {
            return $lawyer->realname ? $lawyer->realname.'律师' : $user->email;
        }

        return $user->email;
    }
}